﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

using AimGameLibrary;
using AimGameLibrary.ScreenManager;

namespace AimGameLibrary.GameObject
{
    public class GameButtonController : GameController
    {
        public Buttons mybuttons;
        StateKey mystatebuttons;
        public StateKey MystatebuttonS { get { return mystatebuttons; } set { mystatebuttons = value; } }

        public override void LoadData(Identification getdata, int IndexData, NodeData node)
        {
            mybuttons = (Buttons)getdata.Items.GetValue(IndexData + 1);
            InputMode = (NodeInputMode)getdata.Items.GetValue(IndexData + 2);

            if (InputMode == NodeInputMode.Isdown_Isup)
            {
                int count = (int)getdata.Items.GetValue(IndexData + 3);
                if (count > 0)
                {
                    String Temp = (String)getdata.Items.GetValue(IndexData + 4);
                    for (int i = 0; i < count; i++)
                        ListNameIsdown.Add(Script.GetName(Temp, count, i));
                }
                count = (int)getdata.Items.GetValue(IndexData + 5);
                if (count > 0)
                {
                    String Temp = (String)getdata.Items.GetValue(IndexData + 6);
                    for (int i = 0; i < count; i++)
                        ListNameIsup.Add(Script.GetName(Temp, count, i));
                }
            }
            else if (InputMode == NodeInputMode.Pressed_Release)
            {
                int count = (int)getdata.Items.GetValue(IndexData + 3);
                if (count > 0)
                {
                    String Temp = (String)getdata.Items.GetValue(IndexData + 4);
                    for (int i = 0; i < count; i++)
                        ListNamePressed.Add(Script.GetName(Temp, count, i));
                }
                count = (int)getdata.Items.GetValue(IndexData + 5);
                if (count > 0)
                {
                    String Temp = (String)getdata.Items.GetValue(IndexData + 6);
                    for (int i = 0; i < count; i++)
                        ListNameRelease.Add(Script.GetName(Temp, count, i));
                }
            }
        }

        public override void HandleInputUpdateGameTime(InputState inputstate, Microsoft.Xna.Framework.GameTime gametime)
        {
            //base.UpdateGameTime(gametime);
            PlayerIndex playerindex;
            if (InputMode == NodeInputMode.Pressed_Release)
            {

                if (inputstate.IsNewButtonPress(this.mybuttons, null, out playerindex))
                {
                    //if (mycontroller.Pressed(this.mykey))
                    foreach (BaseGameObject basegame in ListObjectPressed)
                        basegame.UpdateGameTime(gametime);
                    MystatebuttonS = StateKey.Pressed;
                }
                else MystatebuttonS = StateKey.None;
                if (inputstate.IsNewButtonRelease(this.mybuttons, null, out playerindex))
                {
                    //if (mycontroller.Release(this.mykey))
                    foreach (BaseGameObject basegame in ListObjectRelease)
                        basegame.UpdateGameTime(gametime);
                    MystatebuttonS = StateKey.Release;
                }
                else MystatebuttonS = StateKey.None;
            }
            else if (InputMode == NodeInputMode.Isdown_Isup)
            {
                if (inputstate.IsNewButtonDown(this.mybuttons, null, out playerindex))
                {
                    //if (mycontroller.IsKeyDown(this.mykey))
                    foreach (BaseGameObject basegame in ListObjectIsdown)
                        basegame.UpdateGameTime(gametime);
                    MystatebuttonS = StateKey.IsDown;
                }
                else
                {
                    //else if (mycontroller.IsKeyUp(this.mykey))
                    foreach (BaseGameObject basegame in ListObjectIsup)
                        basegame.UpdateGameTime(gametime);
                    MystatebuttonS = StateKey.IsUp;
                }
            }

        }

        public override void HandleInputUpdateTime(InputState inputstate, TimeSpan timespan)
        {
            //base.UpdateTime(timespan);
            PlayerIndex playerindex;
            if (InputMode == NodeInputMode.Pressed_Release)
            {
                if (inputstate.IsNewButtonPress(this.mybuttons, null, out playerindex))
                {
                    //if (mycontroller.Pressed(this.mykey))
                    foreach (BaseGameObject basegame in ListObjectPressed)
                        basegame.UpdateTime(timespan);
                    MystatebuttonS = StateKey.Pressed;
                }
                else MystatebuttonS = StateKey.None;

                if (inputstate.IsNewButtonRelease(this.mybuttons, null, out playerindex))
                {
                    //if (mycontroller.Release(this.mykey))
                    foreach (BaseGameObject basegame in ListObjectRelease)
                        basegame.UpdateTime(timespan);
                    MystatebuttonS = StateKey.Release;
                }
                else MystatebuttonS = StateKey.None;
            }
            else if (InputMode == NodeInputMode.Isdown_Isup)
            {
                if (inputstate.IsNewButtonDown(this.mybuttons, null, out playerindex))
                {
                    //if (mycontroller.IsKeyDown(this.mykey))
                    foreach (BaseGameObject basegame in ListObjectIsdown)
                        basegame.UpdateTime(timespan);
                    MystatebuttonS = StateKey.IsDown;
                }
                else
                {
                    //else if (mycontroller.IsKeyUp(this.mykey))
                    foreach (BaseGameObject basegame in ListObjectIsup)
                        basegame.UpdateTime(timespan);
                    MystatebuttonS = StateKey.IsUp;
                }
            }

        }

    }
}
